package dev.ece.suit.hexmap;

import java.util.ArrayList;
import java.util.List;

import dev.ece.core.vertex.VAO;
import dev.ece.core.vertex.VBO;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;

@Deprecated
public class HexMapTriangleVAO {

	private VBO verticeVBO;
	
	private VBO uvVBO;
	
	private VBO normalsVBO;
	
	private VBO alphaVBO;
	
	private VAO vao;
	
	public static HexMapTriangleVAO createHexMapTriangleVAO(List<HexMapTriangle> triangles) {
		List<Vector3f> vertice = new ArrayList<Vector3f>();
		List<Vector2f> uv = new ArrayList<Vector2f>();
		List<Vector3f> normals = new ArrayList<Vector3f>();
		List<Vector2f> alpha = new ArrayList<Vector2f>();
		int index = 0;
		for(HexMapTriangle triangle: triangles) {
			vertice.addAll(triangle.getVertice());
			uv.addAll(triangle.getUv());
			normals.addAll(triangle.getNormals());
			for(int i=0; i<triangle.getAlpha().length; i ++) {
				alpha.add(triangle.getAlpha()[i]);
			}
			triangle.setup();
			triangle.setFirst(index * triangle.getVertice().size());
			index ++;
		}
		HexMapTriangleVAO vao = new HexMapTriangleVAO();
		vao.verticeVBO = VBO.newInstance3f(vertice);
		vao.uvVBO = VBO.newInstance2f(uv);
		vao.normalsVBO = VBO.newInstance3f(normals);
		vao.alphaVBO = VBO.newInstance2f(alpha);
		
		vertice.clear();
		uv.clear();
		normals.clear();
		alpha.clear();
		
		vertice = null;
		uv = null;
		normals = null;
		alpha = null;
		return vao;
	}
	
	public void initialize() {
		vao = new VAO();
		vao.bind();
		vao.bindVertexArrayBuffer(verticeVBO, 3);
		vao.bindVertexArrayBuffer(uvVBO, 2);
		vao.bindVertexArrayBuffer(normalsVBO, 3);
		vao.bindVertexArrayBuffer(alphaVBO, 2);
		vao.finish();
		
		verticeVBO = null;
		uvVBO = null;
		normalsVBO = null;
		alphaVBO = null;
	}
	
	public void destroy() {
		if(vao != null) {
			vao.destroy();
		}
	}
	
	public void bind() {
		vao.bind();
	}

	public VAO getVao() {
		return vao;
	}
	
}
